package charles.testproject.xmlparser;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class JDOMParser {
	
	private static Logger logger = Logger.getLogger(JDOMParser.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 这个log4j读取相对路径.properties文件的问题竟然花费了我半个多小时，真是失败。总是忘记在类中，函数外不能
		 * 写操作语句。下面这句操作语句在main()方法外根本就不行。
		 */
		PropertyConfigurator.configure("log/log4jpropertiesfile/log4j.properties");
			System.out.println("***************************J_DOM PARSER***************************");
			long startTime = System.currentTimeMillis();
			try {
				File f = new File("doc/user01.xml");
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(f);
				Element foo = doc.getRootElement();
				List children = foo.getChildren();
				logger.debug("is called");
				for(int i = 0; i < children.size(); i++) {
					//最上面的两句作用是相同的
					//System.out.println("Yesterday = " + ((Element)children.get(i)).getChild("yesterday").getText());
//					System.out.println("name = " + ((Element)children.get(i)).getChildText("name"));
//					System.out.println("age = " + ((Element)children.get(i)).getChildText("age"));
//					System.out.println("sex = " + ((Element)children.get(i)).getChildText("sex"));
				}
			} catch (JDOMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			long endTime = System.currentTimeMillis();
			System.out.println("****J_DOMParser cost time = " + (endTime - startTime));
			System.out.println("***************************J_DOM PARSER***************************");
		}
}
